<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

require_once 'ActionsController.php';

class ActionEventsController extends ActionsController {
    public function beforeFilter(){
        $group = $this->getCurrentUserGroup();
        $this->set('data2', $group);
    }
    public $layout = 'group';
    public $helpers = array('Js' => array('Jquery'), 'Html', 'Form', 'calendar');
    public $uses = array('ActionEvent', 'Group', 'Action','Place');
    var $name = 'ActionEvents';

    public function creerevent() {
        if (!empty($this->request->data)) {

            $data = $this->request->data;
            $date = $data['eventDate'][6] . $data['eventDate'][7] . $data['eventDate'][8] . $data['eventDate'][9] . '-' . $data['eventDate'][3] . $data['eventDate'][4] . '-' . $data['eventDate'][0] . $data['eventDate'][1] . ' 00:00:00';
            $data ['Event']['eventDate'] = $date;
            $Group = $this->getCurrentUserGroup();
            $CityId = $Group['Group']['cities_id'];
            

           // if ($this->ActionEvent->validates()) {
                $action = $this->addAction($data['Event']['Propagation']);  
              //  $this->Action->set($MonAction);
              //  $this->Action->save($MonAction);
                $data ['Event']['actions_id'] = $action['Action']['id'];
                $data ['Event']['cities_id'] = $CityId;
                $dataS = $data['Event'];
                unset($dataS['Propagation']);
                $dataS['nb_user'] = 0;
                
                $this->ActionEvent->set($dataS);
                debug($dataS);
                $Event = $this->ActionEvent->save($dataS);
                debug($Event);
                //$this->propage($action['Action']['id']);
                $this->Session->setFlash('Votre evenement à été créé. ');
                $this->redirect(array('action' => 'home','controller' => 'groups'));
          //}
            
        }
    }
    
    function supp($id=null) {
        
        $this->ActionEvent->delete($id);  
        $this->Session->setFlash('Votre evenement à été supprimée. ');
        $this->redirect(array('action' => 'home','controller' => 'groups'));
    }

    
    function event($id=null) {
        $action=$this->Action->find('first', array(
            'conditions' => array(
                'Action.id' => $id
            )));
      $this->set('a',$action);
        
    }
    function calendar($year = null, $month = null) {

        $this->ActionEvent->recursive = 0;

        $month_list = array('january', 'february', 'march', 'april', 'may', 'june', 'july', 'august', 'september', 'october', 'november', 'december');
        $day_list = array('Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun');
        $base_url = $this->Model . 'ActionEvents/calendar'; // NOT not used in the current helper version but used in the data array
        $groupe = $this->getCurrentUserGroup();
        $city = $groupe['Group']['cities_id'];
        $data = null;

        if (!$year || !$month) {
            $year = date('Y');
            $month = date('M');
            $month_num = date('n');
            $item = null;
        }

        $flag = 0;

        for ($i = 0; $i < 12; $i++) { // check the month is valid if set
            if (strtolower($month) == $month_list[$i]) {
                if (intval($year) != 0) {
                    $flag = 1;
                    $month_num = $i + 1;
                    $month_name = $month_list[$i];
                    break;
                }
            }
        }

        if ($flag == 0) { // if no date set, then use the default values
            $year = date('Y');
            $month = date('M');
            $month_name = date('F');
            $month_num = date('m');
        }

        $fields = array('id', 'name', 'DAY(event_date) AS event_day');

        $var = $this->ActionEvent->find('all', 'MONTH(ActionEvent.eventDate) = ' . $month_num . ' AND ActionEvent.cities_id=' . $city . ' AND YEAR(ActionEvent.eventDate) = ' . $year, $fields, 'ActionEvent.eventDate ASC');

        /**
         * loop through the returned data and build an array of 'events' that is passes to the view
         * array key is the day of month 
         ^
         */
        foreach ($var as $v) {
            
            if (isset($v['ActionEvent']['eventDate'])) {

                $day = $v['ActionEvent']['eventDate'];

                if (isset($data[$day])) {
                    $data[$day] .= '<br /><a href="/Beubeul/ActionEvents/view/' . $v['ActionEvent']['id'] . '">' . $v['ActionEvent']['title'] . '</a>';
                } else {
                    $data[$day] = '<a href="/Beubeul/ActionEvents/view/' . $v['ActionEvent']['id'] . '">' . $v['ActionEvent']['title'] . '</a>';
                }
            }
        }
        $this->set('year', $year);
        $this->set('month', $month);
        $this->set('base_url', $base_url);
        $this->set('data', $data);
        $idCity = $this->getCurrentUserGroup();
        $idCity = $idCity['Group']['cities_id'];
        $MesEvenement = $this->ActionEvent->find('all', array(
            'conditions' => array(
                'cities_id' => $idCity
                )));

        $this->set('MesEvenement', $MesEvenement);
    }

    function mycalendar($year = null, $month = null) {

        $this->ActionEvent->recursive = 0;

        $month_list = array('january', 'february', 'march', 'april', 'may', 'june', 'july', 'august', 'september', 'october', 'november', 'december');
        $day_list = array('Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun');
        $base_url = $this->Model . 'ActionEvents/calendar'; // NOT not used in the current helper version but used in the data array
        $groupe = $this->getCurrentUserGroup();
        $city = $groupe['Group']['cities_id'];
        $data = null;

        if (!$year || !$month) {
            $year = date('Y');
            $month = date('M');
            $month_num = date('n');
            $item = null;
        }

        $flag = 0;

        for ($i = 0; $i < 12; $i++) { // check the month is valid if set
            if (strtolower($month) == $month_list[$i]) {
                if (intval($year) != 0) {
                    $flag = 1;
                    $month_num = $i + 1;
                    $month_name = $month_list[$i];
                    break;
                }
            }
        }

        if ($flag == 0) { // if no date set, then use the default values
            $year = date('Y');
            $month = date('M');
            $month_name = date('F');
            $month_num = date('m');
        }
        $fields = array('id', 'name', 'DAY(event_date) AS event_day');


        $var = $this->ActionEvent->query('Select * from action_events where MONTH(eventDate)=' . $month_num . ' AND YEAR(eventDate)=' . $year . ' and actions_id IN (select id from actions where users_id=' . $this->getCurrentUserId() . ')');


        /**
         * loop through the returned data and build an array of 'events' that is passes to the view
         * array key is the day of month 
         *
         */
        $i = 0;
        foreach ($var as $v) {
            if (isset($v['action_events']['eventDate'])) {

                $day = $v['action_events']['eventDate'];

                if (isset($data[$day])) {
                    $data[$day] .= '<br /><a href="/Beubeul/ActionEvents/view/' . $v['action_events']['id'] . '">' . $v['action_events']['title'] . '</a>';
                } else {
                    $data[$day] = '<a href="/Beubeul/ActionEvents/view/' . $v['action_events']['id'] . '">' . $v['action_events']['title'] . '</a>';
                }
            }
            $i++;
        }

        $this->set('year', $year);
        $this->set('month', $month);
        $this->set('base_url', $base_url);
        $this->set('data', $data);
    }

    public function view($id) {
        $MonEvent = $this->ActionEvent->find('all', array(
            'conditions' => array(
                'id' => $id
                )));
        $action= $this->Action->find('first', array(
            'conditions' => array(
                'Action.id' => $MonEvent[0]['ActionEvent']['actions_id'],
                'users_id' => $this->getCurrentUserId()
                )));
        $this->set('MonEvent', $MonEvent);
        $this->set('action', $action);
    }
    
    public function propage($idAction) {
        
        $action = $this->Action->find('first', array(
            'conditions' => array(
                'Action.id' => $idAction
            )
        ));
        $group = $this->getCurrentUserGroup();
        
        if ($action['Action']['propagationCurrent'] < $action['Action']['propagationMax']){
            
            if ($action['Action']['propagationCurrent'] == 'group'){
                $propa = 'city';
            }
            else if ($action['Action']['propagationCurrent'] == 'city'){
                $propa = 'department';
            }
            else if ($action['Action']['propagationCurrent'] == 'department'){
                $propa = 'state';
            }
            else if ($action['Action']['propagationCurrent'] == 'state'){
                $propa = 'country';
            }
            else if ($action['Action']['propagationCurrent'] == 'country'){
                $propa = 'world';
            }
            
            $action['Action']['places_id'] = $this->getPlacesIdMax($group,$propa);
            $action['Action']['propagationCurrent'] = $propa;
            
            $action = $this->ActionPoll->save('first', array(
                'conditions' => array(
                    'Action.id' => $idAction
                )
            ));
        }
    }
    
}

;
?>

